package library.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import library.dao.IlibraryDao;
import library.db.DbConnection;
import library.pojo.Book;
import library.pojo.BookType;
import library.pojo.BorrowHistory;
import library.pojo.User;
import library.pojo.UserType;

public class BookTypeDaoImpl implements IlibraryDao {

	@Override
	public void save(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Book> get(String isbn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(String isbn) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Book> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 增加书的类型
	 */
	@Override
	public void addBookType(BookType bookType) {
		Connection conn = DbConnection.getConnection();
		String sql = "insert into book_type values(?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, bookType.getIdx());
			ps.setString(2, bookType.getCcs());
			ps.setString(3, bookType.getTypeName());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 删除书的类型
	 */
	@Override
	public void deleteBookType(String idx) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "delete from book_type where IDX = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, idx);
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, null);
		}

	}

	/**
	 * 修改书的类型
	 */
	@Override
	public void updateBookType(BookType bookType) {
		Connection conn = DbConnection.getConnection();
		String sql = "update book_type set CCS=?,type_name=? where IDX=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, bookType.getCcs());
			ps.setString(2, bookType.getTypeName());
			ps.setString(3, bookType.getIdx());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}

	}

	/**
	 * 查询书的类型
	 */
	@Override
	public BookType getBookType(String idx) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from book_type where IDX = ?";
		BookType bookType = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, idx);
			rs = ps.executeQuery();
			while (rs.next()) {
				String bookTypeIdx = rs.getString(1);
				String bookTypeCcs = rs.getString(2);
				String bookTypeName = rs.getString(3);
				bookType = new BookType(bookTypeIdx, bookTypeCcs, bookTypeName);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return bookType;
	}

	/**
	 * 查询书的所有类型
	 */
	@Override
	public List<BookType> findAllBookType() {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from book_Type";
		BookType bookType = null;
		List<BookType> bookTypeList = new ArrayList<BookType>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String bookTypeIdx = rs.getString(1);
				String bookTypeCcs = rs.getString(2);
				String bookTypeName = rs.getString(3);
				bookType = new BookType(bookTypeIdx, bookTypeCcs, bookTypeName);
				bookTypeList.add(bookType);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return bookTypeList;
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteUser(String username) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub

	}

	@Override
	public User getUser(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUserType(UserType userType) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteUserType(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateUserType(UserType userType) {
		// TODO Auto-generated method stub

	}

	@Override
	public UserType getUserType(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserType> findAllUserType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteBorrowHistory(String isbn , String borrowUser) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BorrowHistory getBorrowHistory(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BorrowHistory> findAllBorrowHistory() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 *通过idx获得类型名
	 */
	@Override
	public String getTypeName(String idx) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		String typeName = null ;
		conn = DbConnection.getConnection();
		String sql = "select type_name from book_type where IDX = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, idx);
			rs = ps.executeQuery();
			while (rs.next()) {
				typeName = rs.getString(1);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return typeName;
	}
	/**
	 *通过类型名获得idx
	 */
	public String getIdx(String typeName) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		String idx = null ;
		conn = DbConnection.getConnection();
		String sql = "select IDX from book_type where type_name = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, typeName);
			rs = ps.executeQuery();
			while (rs.next()) {
				idx = rs.getString(1);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return idx;
	}

}
